package top.scsoul.pas.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.scsoul.pas.bean.XhrResponse;
import top.scsoul.pas.entity.Clazz;
import top.scsoul.pas.entity.ExamInfo;
import top.scsoul.pas.entity.User;
import top.scsoul.pas.service.ClazzService;
import top.scsoul.pas.service.ExamInfoService;
import top.scsoul.pas.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
public class ExamInfoController {
    @Autowired
    ExamInfoService examInfoService;
    @Autowired
    ClazzService clazzService;

    @Autowired
    UserService userService;

    @ResponseBody
    @RequestMapping("teacher/addExamInfo")
    public XhrResponse teacherAddExamInfo(ExamInfo examInfo, Long clazzId, HttpSession session) {
        examInfo.setType("单班考试");
        Clazz clazz = clazzService.getById(clazzId);
        ArrayList<Clazz> list = new ArrayList<>();
        list.add(clazz);
        examInfo.setClazzes(list);
        User user = (User) session.getAttribute("user");
        examInfo.setCreator(userService.getUserById(user.getId()));
        examInfo.setCreated(new Date());
        examInfoService.addExamInfo(examInfo);
        return XhrResponse.success();
    }

    @RequestMapping("teacher/examInfo")
    public String teacherExamInfoPage(
            HttpSession session, Model model,
            @RequestParam(required = false) int currentPage,
            @RequestParam(required = false) int pageSize) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        User user = (User) session.getAttribute("user");
        List<ExamInfo> examInfoList = examInfoService.findByCreator(user, currentPage, pageSize);
        log.info("信息{}", examInfoList);
        model.addAttribute("examInfoList", examInfoList);
        return "teacher_examination_information";
    }

    /**
     * teacher考试发布
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("teacher/teacherExaminationRelease")
    public String teacherExaminationRelease(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        List<Clazz> clazzList = userService.getSelfClazz(user.getId());
        model.addAttribute("clazzList", clazzList);
        return "teacher_examination_release";
    }

    /**
     * admin考试发布页面
     *
     * @param model
     * @return
     */
    @RequestMapping("admin/adminExaminationRelease")
    public String adminExaminationRelease(Model model) {
        List<Clazz> clazzList = clazzService.listClazz();
        model.addAttribute("clazzList", clazzList);
        return "admin_examination_release";
    }

    @RequestMapping("admin/adminExaminationManagement")
    public String adminExaminationManagement(
            @RequestParam(required = false) int currentPage,
            @RequestParam(required = false) int pageSize,
            Model model) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        Page<ExamInfo> examInfoPage = examInfoService.findAll(currentPage, pageSize);
        model.addAttribute("examInfoPage", examInfoPage);
        return "admin_examination_management";
    }

    /**
     * 删除考试信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("examInfo/deleteExamination")
    public XhrResponse deleteExamination(Long id) {
        examInfoService.deleteExamInfo(id);
        return XhrResponse.success();
    }
}
